﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.Models/BoxesStateModel
* 类       名: BoxesStateModel
* 功      能:  TD
* 时      间:  2023/7/11 17:51:15
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics.PerformanceData;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace SiliconSelect.Models
{
    public class BoxesStateModel : NotifyBase
    {
        /// <summary>
        /// 料盒编号
        /// </summary>
        private int _boxNumber;

        public int BoxNumber
        {
            get { return _boxNumber; }
            set { SetProperty(ref _boxNumber, value); }
        }

        /// <summary>
        /// 是否正在使用,主要用来判断料盒是否可以进入.比如有好几个空闲料盒的,这个时候PLC有可能还会把料盒清空.
        /// 所以我这里要根据这个状态去判定是否在使用中,如果没有在使用中的料盒.
        /// </summary>
        /// // 料盒对应的状态,主要有三种,空闲,使用中,满料.
        //private int _boxState;

        //public int BoxState
        //{
        //    get { return _boxState; }
        //    set { _boxState = value; }
        //}

        // 上层料盒状态
        private int _upBoxState = (int)BoxesStates.Free;

        public int UpBoxState
        {
            get { return _upBoxState; }
            set
            {
                SetProperty(ref _upBoxState, value);
            }
        }

        // 下层料盒状态
        private int _downBoxState = (int)BoxesStates.Free;

        public int DownBoxState
        {
            get { return _downBoxState; }
            set { SetProperty(ref _downBoxState, value); }
        }



        // 锁框时间
        public double LockedSeconds { get; set; } = 5;

        /// <summary>
        /// 料盒保存的等级类型,可以有多个值,但是这里保存的时候只保存一个,只保存最后一个类型.
        /// </summary>
        private string? _levelType;

        public string? LevelType
        {
            get { return _levelType; }
            set { SetProperty(ref _levelType, value); }
        }

        ///// <summary>
        ///// 
        ///// </summary>
        //private int _countPcUp;

        //public int CountPc
        //{
        //    get
        //    {
        //        return _countPcUp;
        //    }
        //    set
        //    {
        //        _countPcUp = value;
        //        if (_countPcUp == CountMax)
        //        {
        //            // 延时执行这个任务
        //            if (BoxState != (int)BoxesStates.Locked)
        //            {
        //                BoxState = (int)BoxesStates.Locked; // 如果发现是100,就锁框,但是锁多久呢? 锁几片呢.
        //                Task.Run(async () =>
        //                {
        //                    await Task.Delay(Convert.ToInt32(LockedSeconds * 1000));
        //                    BoxState = (int)BoxesStates.Free;
        //                });
        //            }
        //        }
        //    }
        //}

        /// <summary>
        /// PC统计的个数,一般比PLC统计的要超前几个.
        /// </summary>
        private int _upCountPc;

        public int UpCountPc
        {
            get { return _upCountPc; }
            set
            {
                if (value != UpCountPc && value < CountMax && value != 0)
                {
                    UpBoxState = (int)BoxesStates.Using; // 如果值小于CountMax,并且一直在变化就认为这个框在使用用
                }
                else if (value != UpCountPc && value >= CountMax)
                {
                    UpBoxState = (int)BoxesStates.Full; // 如果大于等于最大值,就认为是满状态
                    InsertUpAlarmTosql(); // 将满料报警插入到数据库
                }
                else if (value != UpCountPc && value == 0)
                {
                    UpBoxState = (int)BoxesStates.Free; // 如果等于0,就认为是Free状态.
                }
                SetProperty(ref _upCountPc, value);
            }
        }

        private void InsertUpAlarmTosql()
        {
            string errorCode = "P00";
            switch (BoxNumber)
            {
                case 1:
                    errorCode = "P17";
                    break;
                case 2:
                    errorCode = "P19";
                    break;
                case 3:
                    errorCode = "P21";
                    break;
                case 4:
                    errorCode = "P23";
                    break;
                case 5:
                    errorCode = "P25";
                    break;
                case 6:
                    errorCode = "P27";
                    break;
                case 7:
                    errorCode = "P29";
                    break;
                case 8:
                    errorCode = "P31";
                    break;
                case 9:
                    errorCode = "P33";
                    break;
                case 10:
                    errorCode = "P35";
                    break;
                case 11:
                    errorCode = "P37";
                    break;
                case 12:
                    errorCode = "P39";
                    break;
                case 13:
                    errorCode = "P41";
                    break;
                case 14:
                    errorCode = "P43";
                    break;
                case 15:
                    errorCode = "P45";
                    break;
                case 16:
                    errorCode = "P47";
                    break;
                case 17:
                    errorCode = "P49";
                    break;
                case 18:
                    errorCode = "P51";
                    break;
                default:
                    break;
            }
            if (!string.IsNullOrEmpty(errorCode))
            {
                LogHelper.WriteLog(LogType.Warn, string.Empty, errorCode);
            }
        }

        private void InsertDownAlarmTosql()
        {
            string errorCode = "P00";
            switch (BoxNumber)
            {
                case 1:
                    errorCode = "P18";
                    break;
                case 2:
                    errorCode = "P20";
                    break;
                case 3:
                    errorCode = "P22";
                    break;
                case 4:
                    errorCode = "P24";
                    break;
                case 5:
                    errorCode = "P26";
                    break;
                case 6:
                    errorCode = "P28";
                    break;
                case 7:
                    errorCode = "P30";
                    break;
                case 8:
                    errorCode = "P32";
                    break;
                case 9:
                    errorCode = "P34";
                    break;
                case 10:
                    errorCode = "P36";
                    break;
                case 11:
                    errorCode = "P38";
                    break;
                case 12:
                    errorCode = "P40";
                    break;
                case 13:
                    errorCode = "P42";
                    break;
                case 14:
                    errorCode = "P44";
                    break;
                case 15:
                    errorCode = "P46";
                    break;
                case 16:
                    errorCode = "P48";
                    break;
                case 17:
                    errorCode = "P50";
                    break;
                case 18:
                    errorCode = "P52";
                    break;
                default:
                    break;
            }
            if (!string.IsNullOrEmpty(errorCode))
            {
                LogHelper.WriteLog(LogType.Warn, string.Empty, errorCode);
            }
        }

        /// <summary>
        /// 当前存放的个数,从PLC那边读取.
        /// </summary>
        private int _upCount;

        public int UpCount
        {
            get
            {
                return _upCount;
            }
            set
            {
                if (value != UpCount)
                {
                    if (CountColorIndex == 0)
                    {
                        UpCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                        CountColorIndex = 1;
                    }
                    else
                    {
                        UpCountColor = new SolidColorBrush(Colors.SpringGreen);
                        CountColorIndex = 0;
                    }
                }
                else
                {
                    UpCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                }

                SetProperty(ref _upCount, value);
                // 判断这个值是否变化,如果变化就改变其颜色.

                // CountPc
                if (UpCountPc < UpCount)
                {
                    UpCountPc = UpCount;
                }
                // 如果数量大于等于最大的数量,就证明已经满了.每次赋值的时候去搞定这个事情.
                if (UpCount >= CountMax)
                {
                    UpBoxState = (int)BoxesStates.Full;
                    UpCountColor = new SolidColorBrush(Colors.Red); // 料盒颜色变红
                }
                else if (UpCount == 0) // 如果赋值为0表示空闲.至于什么时候锁定呢.每次赋值为0的时候锁定3秒.
                {
                    UpBoxState = (int)BoxesStates.Free;
                    UpCountPc = 0; // 这个时候Pc的个数也要清空
                    UpCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                }
                //else // 如果不为0就表示正在使用.
                //{
                //    UpBoxState = (int)BoxesStates.Using;
                //    // 去判断下下层的Pc的个数,如果下层的Pc的个数不等于0,也不等于100,就把另外一层也设置成Useing
                //    if (DownBoxState == (int)BoxesStates.Full && DownCount < CountMax && DownCount > CountMax - 10)
                //    {
                //        DownBoxState = (int)BoxesStates.Using;
                //    }
                //}
            }
        }

        private int _downCountPc;

        public int DownCountPc
        {
            get { return _downCountPc; }
            set
            {
                if (value != DownCountPc && value < CountMax && value != 0)
                {
                    DownBoxState = (int)BoxesStates.Using; // 如果值小于CountMax,并且一直在变化就认为这个框在使用用
                }
                else if (value != DownCountPc && value >= CountMax)
                {
                    DownBoxState = (int)BoxesStates.Full; // 如果大于等于最大值,就认为是满状态
                    InsertDownAlarmTosql();
                }
                else if (value != DownCountPc && value == 0)
                {
                    DownBoxState = (int)BoxesStates.Free; // 如果等于0,就认为是Free状态.
                }
                SetProperty(ref _downCountPc, value);
            }
        }

        /// <summary>
        /// 当前存放的个数,从PLC那边读取.
        /// </summary>
        private int _downCount;

        public int DownCount
        {
            get
            {
                return _downCount;
            }
            set
            {
                if (value != DownCount)
                {
                    if (CountColorIndex == 0)
                    {
                        DownCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                        CountColorIndex = 1;
                    }
                    else
                    {
                        DownCountColor = new SolidColorBrush(Colors.SpringGreen);
                        CountColorIndex = 0;
                    }
                }
                else
                {
                    DownCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                }

                SetProperty(ref _downCount, value);
                // 判断这个值是否变化,如果变化就改变其颜色.

                // CountPc
                if (DownCountPc < DownCount)
                {
                    DownCountPc = DownCount;
                }
                // 如果数量大于等于最大的数量,就证明已经满了.每次赋值的时候去搞定这个事情.
                if (DownCount >= CountMax)
                {
                    DownBoxState = (int)BoxesStates.Full;
                    DownCountColor = new SolidColorBrush(Colors.Red); // 料盒颜色变红
                }
                else if (DownCount == 0) // 如果赋值为0表示空闲.至于什么时候锁定呢.每次赋值为0的时候锁定3秒.
                {
                    DownBoxState = (int)BoxesStates.Free;
                    DownCountPc = 0; // PC计数清零
                    DownCountColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EEEEEE"));
                }
                //else // 如果不为0就表示正在使用.
                //{
                //    DownBoxState = (int)BoxesStates.Using;
                //    // 判断另外一层是否是真的满了.
                //    if (UpBoxState == (int)BoxesStates.Full && UpCount < CountMax && UpCount > CountMax - 10)
                //    {
                //        UpBoxState = (int)BoxesStates.Using;
                //    }
                //}
            }
        }

        public int CountColorIndex { get; set; } = 0;

        // 上层料盒的颜色
        private SolidColorBrush? _upCountColor;

        public SolidColorBrush? UpCountColor
        {
            get { return _upCountColor; }
            set { SetProperty(ref _upCountColor, value); }
        }

        private SolidColorBrush? _downCountColor;

        public SolidColorBrush? DownCountColor
        {
            get { return _downCountColor; }
            set { SetProperty(ref _downCountColor, value); }
        }


        /// <summary>
        /// 存放最大个数
        /// </summary>
        private int _countMax;

        public int CountMax
        {
            get { return _countMax; }
            set { SetProperty(ref _countMax, value); }
        }

    }
}
